package com.sright.bcard.enterprise.web.context;

import com.sright.bcard.user.dal.entity.AdminDO;
import com.sright.bcard.user.dal.mapper.AdminDOMapper;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Data
public class UserContext implements ContextWare {

    @Resource
    private AdminDOMapper adminDOMapper;

    private ServletProxy servletProxy;

    public Long getUserId() {
        return servletProxy.getUserId();
    }

    public String getToken() {
        return servletProxy.getToken();
    }

    public void setToken() {
        servletProxy.setToken();
    }

    public Long getEnterpriseId() {
        Long userId = getUserId();
        if (userId == null) {
            return null;
        }

        AdminDO adminDO = adminDOMapper.selectByPrimaryKey(userId);
        if (adminDO == null) {
            return null;
        }

        Long enterpriseId = adminDO.getEnterpriseId();
        return enterpriseId;
    }

    @Override
    public void setContext(ServletProxy servletProxy) {
        this.servletProxy = servletProxy;
    }
}
